var baseUrl = "http://localhost:8080/wishwalls"; 
//var baseUrl = "http://1.iszcloud.sinaapp.com";
var programCodeVal = 'testCode';
$(document).ready(function(){
	queryMenu(programCodeVal);
	$("#menu nav ul.list span").click(function(){
		$(this).addClass("selectd").siblings().removeClass("selectd");
		var ln=$("#menu nav ul.list span").index(this);
		$("#menu nav ul.list li").eq(ln).toggle().siblings("li").hide();
	})
	$("#menu nav ul.list li a").click(function(){
		$(this).addClass("selectd").siblings().removeClass("selectd");
	})
});
function initData(){
	$.ui.launch();
	$.ui.toggleNavMenu();	
	
	$("#protuct_list section div").click(function(){
	   footer_s();
	   var selLength=$(".footers_tree ul").find("img").length;
	   var divno=$(this).parents(".wif_div").attr("id");
	   if($(this).hasClass("sele")){
			$(this).removeClass("sele");
			$(this).parent().find(".img_mv").removeClass("imgmover");
			$(".li_"+divno).html("");
			$(".li_"+divno).removeAttr("class");
		}else if(selLength<3){
			$(this).addClass("sele");
			var tsrc=$(this).parent().find("img").attr("src");
			for(var i=0;i<3;i++){
				if($(".footers_tree ul li").eq(i).html()==""){								
					$(".footers_tree ul li").eq(i).html("<img src='"+tsrc+"'/>");
					$(".footers_tree ul li").eq(i).addClass("li_"+divno);					
	   				$(this).parent().find(".img_mv").addClass("imgmover");
					break;
				}
			}
	   }else{
			$("#task").show();
			$(this).removeClass("sele");
	   }
	})
	$("#menu nav ul li a").click(function(){
		$.ui.toggleSideMenu();
	})
	$(".footers_tree ul li").click(function(){
		$("#"+$(this).attr("class").substring(3)).find("div.sele").removeClass("sele");
		$("#"+$(this).attr("class").substring(3)).find(".img_mv").removeClass("imgmover");
		$(this).html("");
		$(this).removeAttr("class");
	})
	$(".footers_tree a").click(function(){
		$.ui.clearHistory();						
		$.ui.toggleNavMenu();
		$(".footers_tree").hide();
		var totree_img=$(".footers_tree ul").find("img");
		for(var i=0;i<totree_img.length;i++){
			var tsrc=totree_img.eq(i).attr("src");
			$(".the_tree ul li").eq(i).html("<img src=\"images\/pao.png\"><img src='"+tsrc+"'/>");
		}
					
	})
	
	$("a.pro_plist").click(function(){
		$(this).parent().find(".pro_plist_detail").toggle();
	})
	$("a.xuruan").click(function(){
		$("#xuyan").show();
	})
	window.onload=function()
	{	 	
		 protuct_list();
		 tree_list();
		 if($("#pro_detail").css("display")=="block"){
		  	new_swiper();
		  	queryProductionDetail();
		  }
		 window.setInterval(function(){
			  footer_s();
			  if($("#pro_detail").css("display")=="block"){
				  if($("#mywishSwipe>.swipe-wrap").width()==$("#afui").width()){	
					  new_swiper();
				  }
			  }
			  if($("#tree").css("display")=="block"){
				  for(var i=0;i<3;i++)
				  $(".the_tree ul li").eq(i).height($(".the_tree ul li").eq(i).width());
			  }
			  if($("#main").css("display")=="block" && $("#protuct_list .wif_div").eq(0).height()!=$("#afui").width()){				  
			  	 protuct_list()	
			  }
		 	 buttons();
		  }, 200);	
	}
	window.onresize = function () {
		tree_list();
	}
}
function buttons(){
	$("a.button.add_tree,a.button.now_wish").width(($("#afui").width()-30)/2);
	$("a.button.zhichi").click(function(){
		$(this).addClass("zhichi_c").removeClass("zhichi");
		$(this).text("谢谢:)");
	})
}
function footer_s(){
	if($("#main").css("display")=="block" && $("#menu").css("display")!="block"){
	  for(var i=0;i<$(".footers_tree ul li").length;i++){
		  if($(".footers_tree ul li").eq(i).html()==""){								
			  $(".footers_tree").hide();
		  }else{
			  $(".footers_tree").show();
			  break;
		  }
	  }
	}else{
	  $(".footers_tree").hide();
	}
	if($("#tree").css("display")=="block"){
		$("#afui").addClass("tree_show");
	}else{
		$("#afui").removeClass("tree_show");
	}
}
function tree_list(){				
	for(var i=0;i<3;i++){		
		$("#afui.Joyo #tree ul li").eq(i).height($("#afui.Joyo #tree ul li").eq(i).width())
	}
}
function protuct_list(){	
	var margins=10;
	var li_w=($("#afui.Joyo").width()-30)/2;
	//var li=document.getElementById("protuct_list").getElementsByClassName("wif_div");
	var li=$("#protuct_list .wif_div");		   		   
	for(var i=0;i<li.length;i++){
	   li.eq(i).width(li_w);
		var li_height=0;
		if(i<2){			
			//li[i].style.top="10px";
			//li[i].style.left=i*li_w+margins*(i+1)+"px";	
			li.eq(i).css("top","10px");
			li.eq(i).css("left",i*li_w+margins*(i+1));						
		}else if(i%2){				
			//li[i].style.top=li[i-2].offsetHeight+parseInt(li[i-2].style.top)+margins+"px";
			//li[i].style.left=li_w+margins*2+"px";
			li.eq(i).css("top",li.eq(i-2).height()+parseInt(li[i-2].style.top)+margins);
			li.eq(i).css("left",li_w+margins*2);	
		}else{
			//li[i].style.top=li[i-2].offsetHeight+parseInt(li[i-2].style.top)+margins+"px";
			//li[i].style.left="10px";
			li.eq(i).css("top",li.eq(i-2).height()+parseInt(li[i-2].style.top)+margins);
			li.eq(i).css("left","10px");	
		}
	}
	if(!typeof(li) == 'undefined'){
		var aa=li[li.length-1].offsetHeight+parseInt(li[li.length-1].style.top)+margins;
		if(li.length>1){		
			var bb=li[li.length-2].offsetHeight+parseInt(li[li.length-2].style.top)+margins;
			if(aa>bb){
			 $("#protuct_list").height(aa+60);
			}else{
			 $("#protuct_list").height(bb+60);
			}
		}else{
			$("#protuct_list").height(aa+60)
		}
	}
	
}
function new_swiper(){	
	var length=$("#mywishSwipe > .swipe-wrap > div").length;				
	var swiper="";
	for(i=1;i<=length;i++){
		swiper=swiper+"<span></span>";
		$("#mywishSwipe > .swipe-wrap > div").eq(i-1).width($("#afui").width());
	}
	$("#mywishSwipe .swiper-pagination").html(swiper);	
	var bullets = $("#mywishSwipe .swiper-pagination span");		
	bullets[0].className = 'active';		
	var elems = document.getElementById('mywishSwipe');
	window.mywishSwipe = Swipe(elems, {
	//auto: 3000,
	callback: function(index, element) {
		var i = bullets.length;
		while (i--) {
		bullets[i].className = ' ';
		}
		bullets[index].className = 'active';
	},
	transitionEnd: function(index, element) {}
	});;	
	var img_swipt=$("#mywishSwipe > .swipe-wrap > div .pro_swipe").length;
	for(var j=0;j<img_swipt;j++){		
		var elemw = document.getElementById('wishSwipe_'+(j+1));
		switch(j){
		case 0:window.wishSwipe_1 = Swipe(elemw, {});
		case 1:window.wishSwipe_2 = Swipe(elemw, {});
		case 2:window.wishSwipe_3 = Swipe(elemw, {});
		}
	}
		
}
function queryMenu(programCode){
	$.ajax({ 
        type: "post", 
        async: false, 
        url: baseUrl + "/json/wishwall/menu_queryProgramBaseMenu.action", 
        dataType: "json", 
        data: "programCode="+programCode,
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            alert(errorThrown); 
        },
        success: function (data) { 
        	var json=eval(data);
        	var menus = json.menus;
        	var htmlStr = "";
        	for(var i=0;i<menus.length;i++){
        		htmlStr = htmlStr + "<span class='icon colose'>"+menus[i].name+"</span>";
        		htmlStr = htmlStr + "<li>";
        		htmlStr = htmlStr + queryMenuByParentId(programCode,menus[i].id,i);
        		htmlStr = htmlStr + "</li>";
        	}
        	$(".list").html(htmlStr);
        } 
	});
	initData();
}

function queryMenuByParentId(programCode,parentId,parentIndex){
	var htmlStr = "";
	$.ajax({ 
        type: "post",
        async: false, 
        url: baseUrl + "/json/wishwall/menu_queryMenuByParentId.action", 
        dataType: "json", 
        data: "programCode="+programCode+"&parentId="+parentId,
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            alert(errorThrown); 
        },
        success: function (data) { 
        	var json=eval(data);
        	var menus = json.menus;
        	for(var i=0;i<menus.length;i++){
        		if(parentIndex == 0 && i == 0){//初始化第一次的產品列表頁
        			queryProductionByMenuId(programCode,menus[i].id);
        		}
        		htmlStr = htmlStr +"<a href='#main' onclick='queryProductionByMenuIdWithInit(\""+programCode+"\",\""+menus[i].id+"\")'>"+menus[i].name+"</a>";
        	}
        } 
	});
	return htmlStr;
}

function queryProductionByMenuId(programCode,menuId){
	var htmlStr = "";
	$.ajax({ 
        type: "post",
        async: false, 
        url: baseUrl + "/json/wishwall/production_queryProductionByMenuId.action", 
        dataType: "json", 
        data: "programCode="+programCode+"&menuId="+menuId,
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            alert(errorThrown); 
        },
        success: function (data) { 
        	var json=eval(data);
        	var productions = json.productions;
        	for(var i=0;i<productions.length;i++){
        		htmlStr = htmlStr + "<div id='"+productions[i].id+"' class='wif_div'>";
        		htmlStr = htmlStr + "<section>";
        		htmlStr = htmlStr + "<a href='pro_detail.html' data-persist-ajax='true' data-refresh-ajax='true'>";
        		htmlStr = htmlStr + "<img src='"+productions[i].img1Url+"'>";
        		htmlStr = htmlStr + "<h1>"+productions[i].name+"</h1>";
        		htmlStr = htmlStr + "<span>￥"+productions[i].price+"</span>";
        		htmlStr = htmlStr + "</a>";
        		htmlStr = htmlStr + "<div></div>";
        		htmlStr = htmlStr + "</section>";
        		htmlStr = htmlStr + "</div>";
        	}
        } 
	});
	$("#protuct_list").html(htmlStr);
	
}
function queryProductionByMenuIdWithInit(programCode,menuId){
	queryProductionByMenuId(programCode,menuId);
	initData();
}
function saveWishOrder(){
	var productionIdsParam = "";
	for(var i=0;i<$(".footers_tree li").length;i++){
		var imgClass = $(".footers_tree li").eq(i).attr("class");
		if(null != imgClass && "" != imgClass){
			if("" == productionIdsParam){
				productionIdsParam = "productionIds="+imgClass.split("_")[1];
			}else{
				productionIdsParam = productionIdsParam + "&productionIds="+imgClass.split("_")[1];
			}
		}
	}
	$.ajax({ 
        type: "post",
        async: false, 
        url: baseUrl + "/json/wishwall/wishOrder_saveWishOrder.action", 
        dataType: "json", 
        data: productionIdsParam + "&wishOrder.wechatId=jason123"+ "&wishOrder.wishDeclaration="+$("#id_xuyan").val()+"&programCode="+programCodeVal,
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            alert(errorThrown); 
        },
        success: function (data) { 
        	var json=eval(data);
        	if(json.hadExists){
        		alert("已参与过本次活动，谢谢支持。");
        	}else{
        		alert("许愿成功！");	
        	}
        } 
	});
	showHide(this,"xuyan");
}

function getRequestParam() {
   var url = location.search; //获取url中"?"符后的字串
   var theRequest = new Object();
   if (url.indexOf("?") != -1) { 
      var str = url.substr(1);
      strs = str.split("&");
      for(var i = 0; i < strs.length; i ++) { 
         theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
      }
   }
   return theRequest;
}

function queryProductionDetail(){
	var request = getRequestParam();
	$.ajax({ 
        type: "post",
        async: false, 
        url: baseUrl + "/json/wishwall/wishOrder_queryWishOrderById.action", 
        dataType: "json", 
        data: "wishOrderId="+ request["wishOrderId"],
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            alert(errorThrown); 
        },
        success: function (data) { 
        	var json=eval(data);
        	var wishOrderDto = json.wishOrderDto;
        } 
	});
}